package org.ricks.common.conf;

import org.ricks.utils.NodeUtils;
import org.ricks.utils.ObjectUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2020/8/716:54
 */
public class MainConfLoader {

    private org.ricks.common.conf.ConfLoader confLoader;
    private org.ricks.common.conf.ActiveConfLoader activeConfLoader;

    private Map<String, Object> originMainConfValues; // confKey -> confValue
    private Map<String, Object> mainConfValues; // confKey -> confValue
    private Map<String, Object> originMainConfMap; // fileName -> conf
    private Map<String, Object> mainConfMap; // fileName -> conf

    private MainConfLoader() {
    }

    private static class MainConfLoaderInner {
        private static MainConfLoader mainConfLoader = new MainConfLoader();
    }

    public static MainConfLoader getLoader() {
        return MainConfLoaderInner.mainConfLoader;
    }

    public Object load() {
        try {
            (confLoader = org.ricks.common.conf.ConfLoader.getLoader()).loadResult();
            (activeConfLoader = org.ricks.common.conf.ActiveConfLoader.getLoader()).loadResult();
            originMainConfValues = mainConfValues = mergeSources();
            originMainConfMap = mainConfMap = mergeSourceMap();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mainConfValues;
    }

    public Object getMainConfValues() {
        return mainConfValues;
    }

    public Object getOriginMainConfValues() {
        return originMainConfValues;
    }

    public Map<String, Object> getMainConfMap() {
        return mainConfMap;
    }

    public Map<String, Object> getOriginMainConfMap() {
        return originMainConfMap;
    }

    public Object getConfByLoadFileName(org.ricks.common.conf.LoadFileName loadFileName) {
        return getConfByFileName(loadFileName.getFileName());
    }

    public Object getOriginConfByLoadFileName(org.ricks.common.conf.LoadFileName loadFileName) {
        return getOriginConfByFileName(loadFileName.getFileName());
    }

    @SuppressWarnings("unchecked")
    public Object getConfByFileName(String filename) {
        Object source = mainConfMap.get(filename);
        return source;
    }

    public Object getOriginConfByFileName(String filename) {
        if(!originMainConfMap.isEmpty()) {
            Object source = originMainConfMap.get(filename);
            return source;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    private Map<String, Object> mergeSources() {
        Map<String, Object> tmpMainConfValues = new HashMap<>();
        Map<String, Object> confValues = org.ricks.common.conf.ConfLoader.getLoader().getSource();
        Map<String, Object> activeConfValues = org.ricks.common.conf.ActiveConfLoader.getLoader().getSource();
        NodeUtils.merge(tmpMainConfValues, (Map<String, Object>) ObjectUtil.deepCopy(confValues), true);
        NodeUtils.merge(tmpMainConfValues, (Map<String, Object>) ObjectUtil.deepCopy(activeConfValues), true);
        return tmpMainConfValues;
    }

    private Map<String, Object>  mergeSourceMap() {
        Map<org.ricks.common.conf.LoadFileName, Object> confMap = org.ricks.common.conf.ConfLoader.getLoader().getConfMap();
        Map<org.ricks.common.conf.LoadFileName, Object> activeConfMap = org.ricks.common.conf.ActiveConfLoader.getLoader().getConfMap();
        Map<String, Object> tmpMainConfMap = new HashMap<>();
        confMap.forEach(((loadFileName, content) -> tmpMainConfMap.putIfAbsent(loadFileName.getFileName(), content)));
        activeConfMap.forEach(((loadFileName, content) -> tmpMainConfMap.putIfAbsent(loadFileName.getFileName(), content)));
        return tmpMainConfMap;
    }

}
